home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / DOSTIPS3.ZIP / MONOGRFX.TXT < prev    next >
Text File  |  1985-11-24  |  6KB  |  97 lines

  1.               IBM Graphics on a Monochrome Monitor
  2.           (COMPUTE! November 1985 by T. G. Hanlin III)
  3.  
  4.      IBM PCs can generate stunning graphics, but advanced BASIC
  5. graphics features are available only on PCs with a color/graphics
  6. adapter.  However, with the right programming methods, your monochrome
  7. system can produce graphics, too.  They are fairly low resolution, and
  8. no amount of programming skill can make your monochrome monitor display
  9. more than one color, but they are graphics nonetheless.  You may find
  10. them handy for utilitarian purposes or you may enjoy making simple
  11. graphic screens, animated figures, or games.  Once you master the basic
  12. technique, more and more applications will come to mind.
  13.      When a PC boots up, it checks to see if the system contains a
  14. color/graphics adapter and configures itself accordingly.  If a color/
  15. graphics adapter is present, you may use advanced BASIC graphics
  16. commands like PUT and GET.  If not, those commands cause an error.
  17. However, even a monochrome system has the ability to display a large
  18. set of special characters.  IBM graphics characters have ASCII values
  19. of 128 to 255 and include a number of different shapes useful in
  20. creating boxes, borders and so on.
  21.      The characters we're interested in are those which consist of a
  22. solid block.  All computer graphics are produced by turning pixels on
  23. or off to light up different parts of the screen.  The smaller the size
  24. of the pixel dots, the more detailed the image.  Although the IBM
  25. character set doesn't include any pixel-sized characters -- each
  26. character is composed of several pixels -- it does include some we can
  27. use like giant pixels.
  28.      For example, CHR$(219) is a solid block character, the inverse of
  29. CHR$(32), the blank space.  Using these two characters together
  30. provides a graphics screen with 80 x 25 resolution.  To turn a "dot"
  31. within this coarse screen, print the solid block at the desired spot.
  32. To turn off a dot, print a space.  The BASIC function SCREEN(Y,X) tells
  33. you whether a given location contains a dot or an empty space.  Though
  34. you're limited to simple, quite blocky shapes, this system is fast and
  35. simple to use.  However, it's possible to do much better.
  36.      Besides the block and space characters which light up or blank out
  37. an entire screen location, there aer some which light up only part of a
  38. screen position.  For instance, CHR$(220) is solid on the bottom half
  39. and blank on the top.  The reverse is true of CHR$(223).  By using
  40. these characters, we can double our resolution to 80 x 50 pixels.  This
  41. complicates matters a bit, since we want to use only half a screen
  42. position, and BASIC lets you print only to an entire screen position.
  43. Here's a point-plotting routine that handles the tricky details:
  44.  
  45. 10000 GR.Y=Y\2+1:GR.SC=SCREEN(GR.Y,X+1):GR.OFFSET=(Y MOD 2)*3:IF Z=0
  46.       THEN 10020 ELSE IF GR.SC=32 THEN GR.SC=223-GR.OFFSET ELSE IF
  47.       GR.SC+GR.OFFSET<>223 THEN GR.SC=219
  48. 10010 LOCATE GR.Y,X+1:PRINT CHR$(GR.SC);:RETURN
  49. 10020 IF GR.SC+GR.OFFSET=223 THEN GR.SC=32 ELSE IF GR.SC<>32 THEN
  50.       GR.SC=220+GR.OFFSET
  51. 10030 GOTO 10010
  52. 10040 GR.Y=Y\2+1:S9=SCREEN(GR.Y,X+1):Z=(GR.SC=219 OR GR.SC+(Y MOD 2)
  53.       *3=223):RETURN
  54.  
  55.      To plot a point with this routine, set the variable X to the
  56. desired horizontal coordinate (0-79) and the variable Y to the vertical
  57. coordinate (0-49).  Now you've set the screen location for the giant
  58. pixel.  To turn it on, set the variable Z to 1.  Set Z to 0 to turn
  59. the pixel off.  Then call the subroutine with GOSUB 10000.  Line 10040
  60. is a separate routine that tells you whether a given location is lit
  61. up or blank.  To test any point on the screen, set the variables X and
  62. Y to the appropriate coordinates; then GOSUB 10040.  The variable Z
  63. equals -1 if that point is lit or 0 it it's blank.
  64.      Though this system emulates a simple graphics screen, keep in mind
  65. that you are still printing characters.  Thus, there are four screen
  66. locations that cause everything to scroll upward if you plot a point
  67. there: locations (79,46), (79,47), (79,48), and (79,49).  To avoid
  68. scrolling your display, either do not use these particular locations
  69. or restrict your screen to 79 x 50 pixels (use horizontal locations
  70. 0-78).  Note that you can mix text and graphics freely, but putting
  71. graphics on top of text cause some surprising results.  The following
  72. program demonstrates how to animate a simple figure.  Add these lines
  73. to the point-plotting routine and save the program.  Make sure the
  74. numeric keypad is in numeric mode before you run it.
  75.  
  76. 10 KEY OFF:CLS:DEFING A-Z:Y=0:Z=1:FOR X=0 TO 24:SNAKE$=SNAKE$+CHR$(X)
  77.    +CHR$(Y):GOSUB 10000:NEXT:DX=1:DY=0:X=X-1
  78. 20 I$=INKEY$:IF I$<>"" THEN DX=SGN(INSTR("369",I$)-INSTR("147",I$)):
  79.    DY=SGN(INSTR("123",I$)-INSTR("789",I$)):IF I$=" " THEN CLS:END
  80. 30 X=ASC(RIGHT$(SNAKE$,2))+DX:Y=ASC(RIGHT$(SNAKE$,1))+DY:IF X>78 THEN
  81.    X=0 ELSE IF X<0 THEN X=78
  82. 40 IF Y>49 THEN Y=0 ELSE IF Y<0 THEN Y=49
  83. 50 Z=1:GOSUB 10000:SNAKE$=SNAKE$+CHR$(X)+CHR$(Y):X=ASC(LEFT$(SNAKE$,
  84.    1)):Y=ASC(MID$(SNAKE$,2,1)):Z=0:GOSUB 10000:SNAKE$=MID$(SNAKE$,3):
  85.    GOTO 20
  86.  
  87.      Control the direction of the wandering animated snake by using the
  88. numeric keypad.  Press the space bar to end the program.  To improve
  89. its speed the point-plotting routine is as short as possible.  However,
  90. if you don't require fast drawing, you might want to add other
  91. features.  Perhpas you'd like to color or shade the points to
  92. introduce different degrees of brightness (of course, since each
  93. two-pixel pair corresponds to a single character, there's a limit to
  94. this technique).  You might add range checking to check for valid
  95. coordinates before you plot a point.  And you could also modify the
  96. routine to place graphics on top of text correctly.
  97.